package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.adpater.CommentListAdapter;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.javabean.ArticlesBean;
import com.neusoft.movie.v_movies.javabean.CommentBean;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.ArticlesModel;
import com.neusoft.movie.v_movies.model.CollectStateModel;
import com.neusoft.movie.v_movies.model.CommentsModel;
import com.neusoft.movie.v_movies.model.UsersModel;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.movie.v_movies.common.Constants.PHOTO_URL;

public class ArticlesInfoActivity  extends BaseActivity implements RetrofitListener {
    private RecyclerView rv;
    private TextView a_time,u_name,a_name,a_info;
    private ImageView a_pic,left,button_collect,button_comment;
    private int a_id,comment_flag,comment_id,collect_id,collect_flag;
    private Context mContext;
    private String result,author_name;
    private int u_id,author_id;
    private EditText com_content;
    @Override
    void initViews() {
        setLayout(R.layout.activity_articles_info);

        rv = (RecyclerView) findViewById(R.id.rv_comment);
        rv.setLayoutManager(new LinearLayoutManager(this));

        a_name = (TextView) findViewById(R.id.a_name);
        a_pic = (ImageView) findViewById(R.id.a_pic);
        a_time = (TextView) findViewById(R.id.a_time);
        u_name = (TextView) findViewById(R.id.u_name);
        a_info = (TextView) findViewById(R.id.a_info);

        com_content= (EditText) findViewById(R.id.com_content);
        button_collect = (ImageView) findViewById(R.id.button_collect);
        button_comment=(ImageView) findViewById(R.id.button_comment);
        left=(ImageView) findViewById(R.id.left_button);

    }

    @Override
    void initData() {

      //  a_id=getIntent().getIntExtra("a_id",-1);



        Bundle bundle = this.getIntent().getExtras();
        a_id = bundle.getInt("a_id");
        author_id = bundle.getInt("u_id");

        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id=sharedPreferences.getInt("u_id",0);

        getDetail();
        collectstate();//判断收藏文章状态
        getCommentlist();
       // collectauthorstate();//判断收藏用户状态
    }
    private void getDetail()
    {
        ArticlesModel articlesModel = new ArticlesModel();
        articlesModel.getDetail(a_id,this);
    }
    private void getCommentlist()
    {
        CommentsModel commentsModel = new CommentsModel();
        commentsModel.getCommentlist(a_id,1,this);
    }


    private void collectstate()
    {
        CollectStateModel collectStateModel=new CollectStateModel();
        collectStateModel.collectstate(u_id,a_id,1,this);
    }



    private void addcollect()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.addcollect(u_id,a_id,1,this);
    }


    private void addcomments()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.addcomments(u_id,
                a_id,
                1,
                com_content.getText().toString(),this);
    }

    @Override
    void initEvents() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        button_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               addcollect();
            }
        });

        button_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addcomments();
            }
        });

        u_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                        Intent intent = new Intent();
                        intent.setClass(ArticlesInfoActivity.this, AuthorAllArtActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("u_id",author_id);
                        intent.putExtras(bundle);
                        startActivityForResult(intent, 1);

            }
        });

    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch(flag){
            case Constants.GETARTICLEDETAIL:
                ArticlesBean articlesBean=(ArticlesBean) o;
                String pic = PHOTO_URL+articlesBean.getA_pic();
                if(!articlesBean.getA_pic().equals("")){
                    Picasso.with(mContext).load(pic).into(a_pic);
                }

                a_name.setText(articlesBean.getA_name());
                a_time.setText(articlesBean.getA_time());
                a_info.setText(articlesBean.getA_info());
                u_name.setText(articlesBean.getU_name());

                author_id=articlesBean.getU_id();
              //  author_name=articlesBean.getU_name();

                break;

            case Constants.GETMOVIESCOMMENTLIST:
                List<CommentBean> list=(List<CommentBean>)o;
                CommentListAdapter adapter=new CommentListAdapter(this,list);
                rv.setAdapter(adapter);
                break;


            case Constants.ISCOLLECT:
                //获取收藏状态
                ResultBean status=(ResultBean)o;
                result=status.getSuccess();
                if(status.getSuccess().equals("0")){
                    button_collect.setImageResource(R.drawable.ic_collect);  //没收藏
                }
                else
                {
                    button_collect.setImageResource(R.drawable.ic_collect_1);  //收藏
                }

                break;

            case Constants.ADDCOLLECT:
                if (((ResultBean)o).getSuccess().equals("1")) {
                    if (result.equals("0")) {
                        button_collect.setImageResource(R.drawable.ic_collect_1);
                        Toast.makeText(ArticlesInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        button_collect.setImageResource(R.drawable.ic_collect);
                        Toast.makeText(ArticlesInfoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                } else {
                    Toast.makeText(ArticlesInfoActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;

            case Constants.ADDCOMMENTS:
                if (((ResultBean)o).getSuccess().equals("1")) {
                    Toast.makeText(ArticlesInfoActivity.this, "评论成功", Toast.LENGTH_LONG).show();
                    finish();
                } else {
                    Toast.makeText(ArticlesInfoActivity.this, "评论失败", Toast.LENGTH_LONG).show();

                }
        }
    }

    @Override
    public void onFailed() {

    }
}
